package com.hndzyu.yd.my;

import android.annotation.SuppressLint;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.gyf.barlibrary.ImmersionBar;
import com.hndzyu.yd.R;
import com.hndzyu.yd.bean.UserBean;
import com.hndzyu.yd.url.Path;
import com.hndzyu.yd.utils.OkHttpDownloadJsonUtil;
import com.hndzyu.yd.widget.MyGridView;
import com.hndzyu.yd.widget.SignDialog;


import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TiXianActivity extends AppCompatActivity {

    @BindView(R.id.top_back)
    ImageView topBack;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_person_commit)
    TextView tvPersonCommit;
    @BindView(R.id.et_zfb)
    EditText etZfb;
    /*@BindView(R.id.et_money)
    EditText etMoney;*/
    @BindView(R.id.gridView)
    MyGridView gridView;
    @BindView(R.id.alipay)
    RadioButton alipay;
    @BindView(R.id.wx)
    RadioButton wx;
    @BindView(R.id.rg)
    RadioGroup rg;
    @BindView(R.id.bangding)
    LinearLayout bd;
    @BindView(R.id.tv_bd)
    TextView tv_bd;
    @BindView(R.id.tv_moneydetail)
    TextView tv_moneydetail;
    @BindView(R.id.ts)
    TextView ts;
    @BindView(R.id.isfirst)
    TextView isfirst;
    private List<TiXianM> mMList;
    private String money;
    private SignDialog dialog;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ti_xian);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .statusBarColor(R.color.colorAccent)
                .statusBarDarkFont(true, 0.2f)
                .init();
        topTitle.setText("余额提现");
        ts.setSelected(true);
        tvMoney.setText("¥" + UserBean.balance);
        wx.setChecked(true);
        setListener();
        mMList = MoneyList();
        money = mMList.get(0).getMoney().substring(0, mMList.get(0).getMoney().length() - 1);
        gridView.setAdapter(new TiXianAdapter(this, mMList));

        dialog = new SignDialog(TiXianActivity.this);
        dialog.setListener(new SignDialog.SignListener() {
            @Override
            public void checktrue() {
                dialog.dismiss();
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                cm.setText("dingzhiyuyidian001");
                Toast.makeText(TiXianActivity.this, "客服号复制成功", Toast.LENGTH_SHORT).show();
            }
        });

        bd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (flag) {
                    dialog.show();
                    dialog.showImg(R.mipmap.dialog_tx);
                    dialog.showTX("绑定微信公众号", "微信搜索和关注公众号,绑定后即可进行提现<br><font color='#149CF3'>亿点平台(dingzhiyuyidian001)</font>");
                }
            }
        });
//        if (UserBean.gao == 0) {
//            isfirst.setText("首次2元秒提现");
//        } else {
//            isfirst.setText("50元以下自动提现");
//        }
    }

    boolean flag = false;
    double yuji = 0;
    double xian = 0;
    String txkefu = "";

    @Override
    protected void onResume() {
        super.onResume();
        OkHttpDownloadJsonUtil.downloadJson(this, Path.money + "?uid=" + UserBean.uid, new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                if (!"1".equals(json) && !"2".equals(json)) {
                    try {
                        JSONObject object = new JSONObject(json);
                        JSONObject info = object.getJSONObject("info");
                        yuji = object.getDouble("sum");
                        xian = object.getDouble("xian");
                        UserBean.wx = info.getString("kefuwx");
                        JSONObject kefuobject = object.getJSONObject("kefu");
                        txkefu = kefuobject.optString("drawwx").split(",")[0];
                        tv_moneydetail.setText("可提金额:¥" + xian + "  预计收入:¥" + yuji);
                        ts.setText(kefuobject.optString("drawtext"));
                        if ("".equals(info.optString("openid"))) {
                            flag = true;
                        } else {
                            tv_bd.setText("已绑定");
                            flag = false;
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    private void setListener() {
        topBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        tvPersonCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (flag) {
                    dialog.show();
                    dialog.showImg(R.mipmap.dialog_tx);
                    dialog.showTX("绑定微信公众号", "微信搜索和关注公众号,绑定后即可进行提现<br><font color='#149CF3'>亿点平台(dingzhiyuyidian001)</font>");
                } else {
                    if (!"".equals(etZfb.getText().toString())) {
//                        if (Double.parseDouble(money) > Double.parseDouble(UserBean.balance)) {
//                            Toast.makeText(TiXianActivity.this, "提现金额不得大于当前余额", Toast.LENGTH_SHORT).show();
//                        } else {
                        if (alipay.isChecked()) {
                            tixian(UserBean.uid, "支付宝：" + etZfb.getText().toString(), money, 1);
                        } else if (wx.isChecked()) {
                            tixian(UserBean.uid, "微信：" + etZfb.getText().toString(), money, 0);
                        }
                    } else {
                        Toast.makeText(TiXianActivity.this, "请输入提现账号", Toast.LENGTH_SHORT).show();
                    }
                }

            }

        });
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.alipay:
                        if (UserBean.gao == 0) {
                            alipay.setEnabled(false);
                            wx.setChecked(true);
                            Toast.makeText(TiXianActivity.this, "首次提现仅限微信", Toast.LENGTH_SHORT).show();
                        } else {
                            etZfb.setHint("请输入支付宝账号");
                            etZfb.setText("");
                        }
                        break;
                    case R.id.wx:
                        etZfb.setHint("必须填写微信号");
                        ;
                        break;
                    default:
                        break;
                }
            }
        });
    }

    private void tixian(int uid, String zfb, String money, int type) {
        if (Double.parseDouble(money) <= xian) {
            Log.i("_________", Path.manualWithdrawal + "?uid=" + uid
                    + "&acount=" + money + "&address=" + zfb + "&type=" + type);
            OkHttpDownloadJsonUtil.downloadJson(TiXianActivity.this, Path.manualWithdrawal + "?uid=" + uid
                    + "&acount=" + money + "&address=" + zfb + "&type=" + type, new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {

                @Override
                public void onsendJson(String json) {

                    if (!"1".equals(json) && !"2".equals(json)) {
                        try {
                            Log.i("______", json);
                            JSONObject jsonObject = new JSONObject(json);

                            if (jsonObject.has("status")) {
                                final SignDialog dialog = new SignDialog(TiXianActivity.this);
                                dialog.setListener(new SignDialog.SignListener() {
                                    @Override
                                    public void checktrue() {
                                        dialog.dismiss();
                                        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                                        cm.setText(txkefu);
                                        Toast.makeText(TiXianActivity.this, "客服号复制成功", Toast.LENGTH_SHORT).show();
                                    }
                                });
                                dialog.show();
                                dialog.showImg(R.mipmap.dialog_tx);
                                if (jsonObject.getInt("status") == 1) {
                                    dialog.showTX("提现申请已提交", "如有问题请联系客服微信<br><font color='#149CF3'>" + txkefu + "</font>");
                                } else {
                                    dialog.showTX(jsonObject.getString("info"), "如有问题请联系客服微信<br><font color='#149CF3'>" + txkefu + "</font>");
                                }

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        Toast.makeText(TiXianActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        } else {
            AlertDialog.Builder builder = new AlertDialog.Builder(TiXianActivity.this);
            builder.setTitle("余额不足");
            builder.setMessage("可提金额为" + xian + "元,还有" + yuji + "元在路上");
            builder.setPositiveButton("知道了", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                }
            });
            builder.show();
        }
    }

    private List<TiXianM> MoneyList() {
        List<TiXianM> datas = new ArrayList<>();
        datas.add(new TiXianM(1, "100元", true));
        datas.add(new TiXianM(2, "200元", false));
        datas.add(new TiXianM(3, "300元", false));
        datas.add(new TiXianM(4, "400元", false));
        datas.add(new TiXianM(5, "500元", false));
        datas.add(new TiXianM(6, "600元", false));
        return datas;
    }

    class TiXianM {
        private int id;
        private String money;
        private boolean check;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getMoney() {
            return money;
        }

        public void setMoney(String money) {
            this.money = money;
        }

        public boolean isCheck() {
            return check;
        }

        public void setCheck(boolean check) {
            this.check = check;
        }

        public TiXianM() {
        }

        public TiXianM(int id, String money, boolean check) {
            this.id = id;
            this.money = money;
            this.check = check;
        }
    }

    private class TiXianAdapter extends BaseAdapter {
        private List<TiXianM> datas;
        private Context mContext;

        public TiXianAdapter(Context mContext, List<TiXianM> datas) {
            this.mContext = mContext;
            this.datas = datas;
        }

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public Object getItem(int i) {
            return datas.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            ViewHolder vh;
            if (view == null) {
                vh = new ViewHolder();
                view = LayoutInflater.from(mContext).inflate(R.layout.item_tixian, null);
                vh.rb = (RadioButton) view.findViewById(R.id.rb);
                view.setTag(vh);
            } else {
                vh = (ViewHolder) view.getTag();
            }
            vh.rb.setText(datas.get(i).getMoney());
            if (datas.get(i).isCheck()) {
                vh.rb.setChecked(true);
            } else {
                vh.rb.setChecked(false);
            }
            vh.rb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    for (int j = 0; j < datas.size(); j++) {
                        datas.get(j).setCheck(false);
                    }
                    datas.get(i).setCheck(true);
                    notifyDataSetChanged();
                    money = datas.get(i).getMoney().substring(0, datas.get(i).getMoney().length() - 1);
                }
            });
            return view;
        }

        class ViewHolder {
            public RadioButton rb;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}